# -*- coding: utf-8 -*-
"""
Created on Sat Apr  4 20:35:21 2020

@author: nchji
"""

def get_mutilbar_ta(obj,ta_args={},extra_periods=[5,15],datatype="df"):
    import pandas as pd
    from jili.price.kline import barset_research
    rst = []
    from jili.data.db import getk1mbar_pkl
    k1m = getk1mbar_pkl(obj)
    barset = barset_research("rb1801", extra_periods=p, ta_args=ta_args)
    date0=k1m[0]["timekey"]
    barset.gethis1dbar(date0)
    for i in k1m:
        rst.append(barset.onbar(i))
    return pd.DataFrame(rst)
def research_tag(a,p):
    p0=["1m","1d"]
    for i in p:
        k=str(i)+"m"
        p0.append(k)
    for i in p0:
        a["sigma."+i]=a["middleband."+i]-a["upperband1."+i]
        a["MACD_v."+i]=a["MACD."+i].diff(1)
        a["MACD_vv."+i]=a["MACD_v."+i].diff(1)
        a["MA5_v."+i]=a["MA5."+i].diff(1)
        a["MA10_v."+i]=a["MA10."+i].diff(1)
        a["MA20_v."+i]=a["MA20."+i].diff(1)
        a["MA40_v."+i]=a["MA40."+i].diff(1)
        a["MA60_v."+i]=a["MA60."+i].diff(1)
        a["obv_v."+i]=a["obv."+i].diff(1)
        a["rsi12_v."+i]=a["rsi12."+i].diff(1)
        a["SAR2_c."+i]=a["close."+i]-a["SAR2."+i]
        a["SAR4_c."+i]=a["close."+i]-a["SAR4."+i]
        a["SAR6_c."+i]=a["close."+i]-a["SAR6."+i]

        
if __name__=="__main__":
    #"""
    ta_args = [{"cmd": "BBANDS", "out": ["upperband3", "middleband", "lowerband3"],
                "arg": {"timeperiod": 9, "nbdevup": 3, "nbdevdn": 3, "matype": 0}, "batch": 9},
               {"cmd": "BBANDS", "out": ["upperband2", "_", "lowerband2"],
                "arg": {"timeperiod": 9, "nbdevup": 2, "nbdevdn": 2, "matype": 0}, "batch": 9},
               {"cmd": "BBANDS", "out": ["upperband1", "_", "lowerband1"],
                "arg": {"timeperiod": 9, "nbdevup": 1, "nbdevdn": 1, "matype": 0}, "batch": 9},
               {"cmd": "MACD", "out": ["DIF","DEA","MACD"], "arg": {"fastperiod": 12, "slowperiod": 26, "signalperiod": 9}, "batch": 34},
               {"cmd": "SMA", "out": ["MA5"], "arg": {"timeperiod": 5}, "batch": 5},
               {"cmd": "SMA", "out": ["MA10"], "arg": {"timeperiod": 10}, "batch": 10},
               {"cmd": "SMA", "out": ["MA20"], "arg": {"timeperiod": 20}, "batch": 20},
               {"cmd": "SMA", "out": ["MA40"], "arg": {"timeperiod": 40}, "batch": 40},
               {"cmd": "SMA", "out": ["MA60"], "arg": {"timeperiod": 60}, "batch": 60},
               {"cmd": "OBV", "out": ["obv"], "batch": 1},
               {"cmd": "RSI", "out": ["rsi12"], "arg": {"timeperiod": 12}, "batch": 13},
               {"cmd": "KDJ", "out": ["K", "D", "J"], "arg": {"timeperiod":9,"m1":3,"m2":3}, "batch": 13},
               {"cmd": "SAR", "out": ["SAR2"], "arg": {"acceleration": 0.02, "maximum": 0.2}, "batch": 2},
               {"cmd": "SAR", "out": ["SAR4"], "arg": {"acceleration": 0.02, "maximum": 0.2}, "batch": 4},
               {"cmd": "SAR", "out": ["SAR6"], "arg": {"acceleration": 0.02, "maximum": 0.2}, "batch": 6},
               {"cmd": "hl_point_shape", "out": ["prehigh", "prehightimekey", "prelow","prelowtimekey"], "arg": {"fx": "up"}, "batch": 1}
               ]
    """
    ta_args = [{"cmd": "KDJ", "out": ["K", "D", "J"], "arg": {"timeperiod":9,"m1":3,"m2":3}, "batch": 13},
               {"cmd": "KDJ1", "out": ["K1", "D1", "J1"], "arg": {"timeperiod":9,"m1":3,"m2":3}, "batch": 13}]
    """
    #p = [5,10,15,30,60]
    p=[]
    a=get_mutilbar_ta("rb1710",ta_args=ta_args,extra_periods=p)
    #research_tag(a,p)
    

